package com.zhuo.project.order.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhuo.framework.aspectj.lang.annotation.Log;
import com.zhuo.framework.aspectj.lang.enums.BusinessType;
import com.zhuo.framework.redis.RedisCache;
import com.zhuo.project.order.domain.ZzTask;
import com.zhuo.project.order.dto.k3.ReturnK3Dto;
import com.zhuo.project.order.dto.k3.SendK3Dto;
import com.zhuo.project.order.service.IZzTaskService;
import com.zhuo.framework.web.controller.BaseController;
import com.zhuo.framework.web.domain.AjaxResult;
import com.zhuo.common.utils.poi.ExcelUtil;
import com.zhuo.framework.web.page.TableDataInfo;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * AdEcom任务Controller
 * 
 * @author zhuo
 * @date 2020-03-26
 */
@Api("订单任务")
@RestController
@RequestMapping("/order/task")
public class ZzTaskController extends BaseController
{
    @Autowired
    private IZzTaskService zzTaskService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询AdEcom任务列表
     */
    @PreAuthorize("@ss.hasPermi('order:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZzTask zzTask)
    {
        startPage();
        List<ZzTask> list = zzTaskService.selectZzTaskList(zzTask);
        return getDataTable(list);
    }

    @ApiOperation("获取任务明细")
    @ApiImplicitParam(name = "refId", value = "refId", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('order:task:list')")
    @GetMapping(value = "/info/{refId}")
    public AjaxResult getTaskInfo(@PathVariable("refId") String refId)
    {
        ZzTask zzTask = new ZzTask();
        zzTask.setRefId(refId);
        zzTask = zzTaskService.selectZzTaskList(zzTask).get(0);
        String key = "";
        if(zzTask.getType()== 40 ){
            key = "zhuozheng:k3:send:"+ refId;
        }else {
            key = "zhuozheng:k3:return:"+ refId;
        }
        String queryParam = redisCache.getCacheObject(key);
        if(zzTask.getType()== 40 ){
            SendK3Dto sendK3Dto = JSONUtil.toBean(queryParam,SendK3Dto.class);
            return AjaxResult.success(sendK3Dto);
        }else {
            ReturnK3Dto returnK2Dto = JSONUtil.toBean(queryParam,ReturnK3Dto.class);
            return AjaxResult.success(returnK2Dto);
        }

    }





    /**
     * 导出AdEcom任务列表
     */
    @PreAuthorize("@ss.hasPermi('order:task:export')")
    @Log(title = "AdEcom任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZzTask zzTask)
    {
        List<ZzTask> list = zzTaskService.selectZzTaskList(zzTask);
        ExcelUtil<ZzTask> util = new ExcelUtil<ZzTask>(ZzTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取AdEcom任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zzTaskService.selectZzTaskById(id));
    }

    /**
     * 新增AdEcom任务
     */
    @PreAuthorize("@ss.hasPermi('order:task:add')")
    @Log(title = "AdEcom任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZzTask zzTask)
    {
        return toAjax(zzTaskService.insertZzTask(zzTask));
    }

    /**
     * 修改AdEcom任务
     */
    @PreAuthorize("@ss.hasPermi('order:task:edit')")
    @Log(title = "AdEcom任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZzTask zzTask)
    {
        return toAjax(zzTaskService.updateZzTask(zzTask));
    }

    /**
     * 删除AdEcom任务
     */
    @PreAuthorize("@ss.hasPermi('order:task:remove')")
    @Log(title = "AdEcom任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zzTaskService.deleteZzTaskByIds(ids));
    }
}
